package com.situ.student.service.impl;

import java.util.List;

import com.situ.student.dao.ICourseDao;
import com.situ.student.dao.impl.CourseDaoImpl;
import com.situ.student.entity.Course;
import com.situ.student.entity.PageBean;
import com.situ.student.service.ICourseService;

public class CourseServiceImpl implements ICourseService {
	private ICourseDao courseDao =  new CourseDaoImpl();
	
	
	@Override
	public List<Course> list() {
		return courseDao.list();
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = courseDao.deleteById(id);
		return count == 1 ? true : false; //返回true 或 falae
	}

	@Override
	public PageBean<Course> getPagBean(int pageNo, int pageSize) {
		//new对象
		PageBean<Course> pageBean = new PageBean<Course>();
		//当前页数
		pageBean.setPageNo(pageNo);
		//页的数据
		pageBean.setPageSize(pageSize);
		//总的记录数
		int totalCount = courseDao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		//一共多少页
		int totalPage = (int)Math.ceil((double)totalCount/pageSize);
		pageBean.setTotalPage(totalPage);
		//当前页的数据
		int offset = (pageNo - 1) * pageSize;
		List<Course> list = courseDao.pageList(offset,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public boolean checkName(String name) {
		int count =  courseDao.selectCountByName(name);
		return count > 0 ? true : false;
	}

	@Override
	public boolean insert(Course course) {
		int count = courseDao.insert(course);
		return count == 1 ? true : false;
	}
	
	//展示页面
	@Override
	public Course findById(Integer id) {
		return courseDao.findById(id);
	}
	
	//修改
	@Override
	public boolean update(Course course) {
		int count = courseDao.update(course);
		return count == 1 ? true : false;
	}
	//批量删除
	@Override
	public boolean deleteAll(String[] selectIds) {
		int count = courseDao.deleteAll(selectIds);
		return count == selectIds.length ? true : false;
	}

}
